<?php

namespace App\Console\Commands;

use App\Jobs\HandleToTask;
use App\Services\TaskService;
use Carbon\Carbon;
use Illuminate\Console\Command;

class TaskTodo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'task:todo';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     * @throws \Exception
     */
    public function handle()
    {
        // 获取当前时间执行的任务
        $tasks = app(TaskService::class)->makeModel()->where('date', Carbon::now()->format('Y-m-d H:i:00'))->get();
        try {
            if ($tasks->count() > 0) {
                \Log::info('开始队列，自动任务数量为：' . $tasks->count());
            }
            foreach ($tasks as $task) {
                // 丢队列
                HandleToTask::dispatch($task);
            }
        } catch (\Exception $e) {
            \Log::info($e->getMessage());
        }
    }
}
